<?php

namespace common\models;

use backend\models\User;
use Yii;

/**
 * This is the model class for table "project_team".
 *
 * @property string $id
 * @property string $project_id
 * @property string $manager
 * @property string $owner
 * @property string $partner
 * @property string $explain
 * @property string $lock_user
 * @property string $lock_timestamp
 * @property string $updated_at
 */
class ProjectTeam extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'project_team';
    }

    /*关联项目表*/
    public function getProject()
    {
        return $this->hasOne(Project::className(), ['id' => 'project_id']);
    }

    /*关联项目表*/
    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'owner']);//manager
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['project_id'], 'required'],
            //[['project_id', 'manager', 'owner', 'partner', 'explain', 'lock_user'], 'required'],
            [['project_id', 'lock_user'], 'integer'],
            [['explain'], 'string'],
            [['owner'], 'default', 'value' => 0],
            [['updated_at', 'owner', 'manager', 'partner',], 'safe'],
            [['lock_timestamp'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'project_id' => 'Project ID',
            'manager' => 'Manager',
            'owner' => 'Owner',
            'partner' => 'Partner',
            'explain' => 'Explain',
            'lock_user' => 'Lock User',
            'lock_timestamp' => 'Lock Timestamp',
            'updated_at' => 'Updated At',
        ];
    }

    public function getUserId($users)
    {
        $arr_users = explode('，', $users);
        if (count($arr_users) > 0) {
            $users = implode('\',\'', $arr_users);
            $arr_user_ids = User::find()->where("`realname` IN ('" . $users . "')")->column();
            return implode(',', $arr_user_ids);
        } else {
            return '';
        }

    }

}
